package com.example.teamtribe.main.club.clubintroduce.media;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.main.club.clubintroduce.media.photo.PhotoActivity;
import com.example.teamtribe.main.club.clubintroduce.media.video.VideoActivity;
import com.example.teamtribe.utils.Utility;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lenovo on 2018/6/3.
 */

public class MediaGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public static final int ADD = 1;
    public static final int MEDIA = 2;

    private Context context;
    private List<Media> mediaList;
    private int teamId;
    private int type;

    public MediaGridAdapter(int teamId,int type){
        mediaList = new ArrayList<>();
        this.teamId=teamId;
        this.type=type;
    }
    public void addMedia(Media media) {
        mediaList.add(media);
        Collections.sort(mediaList);
    }
    public void setMediaList(List<Media> medias) {
        mediaList=medias;
        Collections.sort(mediaList);
    }
    public void clear(){
        if(mediaList!=null) {
            mediaList.clear();
        }
    }
    static class MediaViewHolder extends RecyclerView.ViewHolder {
        ImageView cover;
        TextView name;

        public MediaViewHolder(View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.iv_cover);
            name = itemView.findViewById(R.id.tv_name);
        }
    }
    static class AddViewHolder extends RecyclerView.ViewHolder {

        public AddViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        final RecyclerView.ViewHolder holder;
        if(viewType==MEDIA){
            final View view = LayoutInflater.from(context).inflate(R.layout.item_club_media, parent, false);
            holder = new MediaGridAdapter.MediaViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    Intent intent;
                    if(type==1)
                        intent=new Intent(context, PhotoActivity.class);
                    else
                        intent=new Intent(context, VideoActivity.class);
                    intent.putExtra("media",mediaList.get(position-1));
                    context.startActivity(intent);
                }
            });
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.item_club_media_add, parent, false);
            holder = new MediaGridAdapter.AddViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, AddMediaActivity.class);
                    intent.putExtra("teamId",teamId);
                    intent.putExtra("type",type);
                    context.startActivity(intent);
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof MediaGridAdapter.MediaViewHolder){
            Media media=mediaList.get(position-1);
            ((MediaViewHolder) holder).name.setText(media.getName());
            Glide.with(context).load(Utility.getUri(media.getCover())).into(((MediaViewHolder) holder).cover);
        }
    }

    @Override
    public int getItemCount() {
        return mediaList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){return ADD;}
        else {return MEDIA;}
    }
}
